package com.gywj.race.controller.api;

import com.gywj.race.config.result.AjaxResultHandler;
import com.gywj.race.dto.*;
import com.gywj.race.entity.ParticipantInfoAuditStatus;
import com.gywj.race.entity.ParticipantInfoBaseBestScore;
import com.gywj.race.entity.ParticipantPageParam;
import com.gywj.race.entity.ParticipantTeam;
import com.gywj.race.service.PaticipantService;
import com.gywj.race.util.ObjectUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * The type Paticipant controller.
 */
@RestController
public class PaticipantController {
    @Resource
    private PaticipantService paticipantService;


    /**
     * Add participant info base best score map.
     *
     * @param paticipantInfoBaseBestScore the paticipant info base best score
     * @return the map
     */
    @PostMapping("/participant/info/base/best/score/add")
    public Map<String, Object> addParticipantInfoBaseBestScore(@RequestBody ParticipantInfoBaseBestScore paticipantInfoBaseBestScore) {
        try {
            paticipantService.addParticipantInfoBaseBestScore(paticipantInfoBaseBestScore);
            return AjaxResultHandler.Success("添加成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Gets participant info audit status list.
     *
     * @return the participant info audit status list
     */
    @PostMapping("/participant/info/audit/status/list")
    public Map<String, Object> getParticipantInfoAuditStatusList() {
        try {
            List<ParticipantInfoAuditStatus> participantInfoAuditStatusList = paticipantService.getParticipantInfoAuditStatusList();
            return AjaxResultHandler.Success("查询成功", participantInfoAuditStatusList);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Add participant team map.
     *
     * @param participantTeam the participant team
     * @return the map
     */
    @PostMapping("/participant/team/add")
    public Map<String, Object> addParticipantTeam(@RequestBody ParticipantTeam participantTeam) {
        try {
            paticipantService.addParticipantTeam(participantTeam);
            return AjaxResultHandler.Success("添加成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * Gets participant team list by event id.
     *
     * @param eventId the event id
     * @return the participant team list by event id
     */
    @PostMapping("/participant/team/list/by/eventId")
    public Map<String, Object> getParticipantTeamListByEventId(@RequestParam("eventId") Integer eventId) {
        try {
            List<ParticipantTeam> participantTeamListByEventId = paticipantService.getParticipantTeamListByEventId(eventId);
            return AjaxResultHandler.Success("查询成功", participantTeamListByEventId);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Sign up map.
     *
     * @param paticipantAddDto the paticipant add dto
     * @param request          the request
     * @return the map
     */
    @PostMapping("/participant/sign/up")
    public Map<String, Object> signUp(@RequestBody PaticipantDto paticipantAddDto, HttpServletRequest request) {
        try {

            Map<String, Object> resultMap = paticipantService.addParticipantInfo(paticipantAddDto, request);
            return AjaxResultHandler.Success("报名成功", resultMap);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Wxpay map.
     *
     * @param payDto the pay dto
     * @return the map
     */
    @PostMapping("/participant/wxpay")
    public Map<String, Object> wxpay(@RequestBody PayDto payDto) {
        try {
            Integer eventPriceId = payDto.getEventPriceId();
            Integer pib = payDto.getPib();
            Integer tradeType = payDto.getTradeType();
            String openId = payDto.getOpenId();
            Map<String, String> resultMap = paticipantService.wxpay(pib, eventPriceId, tradeType, openId);
            return AjaxResultHandler.Success("支付成功", resultMap);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @PostMapping("/participant/pay/zero")
    public Map<String, Object> wxpayZero(@RequestBody PayDto payDto) {
        try {
            Integer eventPriceId = payDto.getEventPriceId();
            Integer pib = payDto.getPib();
            Integer tradeType = payDto.getTradeType();
            paticipantService.payZero(pib, eventPriceId, tradeType);
            return AjaxResultHandler.Success("支付成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Cancel order map.
     *
     * @param out_trade_no the out trade no
     * @return the map
     */
    @PostMapping("participant/wxpay/cancelOrder")
    public Map<String, Object> cancelOrder(@RequestParam String out_trade_no) {
        try {
            paticipantService.cancelOrder(out_trade_no);
            return AjaxResultHandler.Success("成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Notice string.
     * 异步通知接口
     *
     * @param request the request
     * @return the string
     * @throws Exception the exception
     * @author ccg
     * @since 2018.11.29
     */
    @PostMapping("participant/wxpay/notice")
    @ApiOperation(value = "异步通知接口", notes = "异步通知接口")
    public String notice(HttpServletRequest request) {
        try {
            return paticipantService.notice(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    /**
     * Pay success map.
     *
     * @param infoId  the info id
     * @param raceId  the race id
     * @param tradeNo the trade no
     * @param request the request
     * @return the map
     */
    @PostMapping("participant/pay/success")
    public Map<String, Object> paySuccess(@RequestParam Integer infoId, @RequestParam Integer raceId, @RequestParam String tradeNo, HttpServletRequest request) {
        try {
            paticipantService.paySuccess(infoId, raceId, tradeNo, request);
            return AjaxResultHandler.Success("待审核状态更新成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Refuse map.
     *
     * @param infoId  the info id
     * @param raceId  the race id
     * @param request the request
     * @return the map
     */
    @PostMapping("participant/refuse")
    public Map<String, Object> refuse(@RequestParam Integer infoId, @RequestParam Integer raceId, HttpServletRequest request) {
        try {
            paticipantService.refuse(infoId, raceId, request);
            return AjaxResultHandler.Success("驳回状态更新成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Update participant info map.
     *
     * @param paticipantAddDto the paticipant add dto
     * @param request          the request
     * @return the map
     */
    @PostMapping("participant/update")
    public Map<String, Object> updateParticipantInfo(@RequestBody PaticipantDto paticipantAddDto, HttpServletRequest request) {
        try {
            paticipantService.updateParticipantInfo(paticipantAddDto, request);
            return AjaxResultHandler.Success("重填信息成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Pass map.
     *
     * @param infoId  the info id
     * @param raceId  the race id
     * @param request the request
     * @return the map
     */
    @PostMapping("participant/pass")
    public Map<String, Object> pass(@RequestParam Integer infoId, @RequestParam Integer raceId, HttpServletRequest request) {
        try {
            paticipantService.pass(infoId, raceId, request);
            return AjaxResultHandler.Success("通过报名，等待比赛");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Pass map.
     *
     * @param infoId  the info id
     * @param raceId  the race id
     * @param request the request
     * @return the map
     */
    @PostMapping("result/pass")
    public Map<String, Object> resultPass(@RequestParam Integer infoId, @RequestParam Integer raceId, HttpServletRequest request) {
        try {
            paticipantService.resultPass(infoId, raceId, request);
            return AjaxResultHandler.Success("通过成绩审核");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Refuse map.
     *
     * @param infoId  the info id
     * @param raceId  the race id
     * @param request the request
     * @return the map
     */
    @PostMapping("result/refuse")
    public Map<String, Object> resultRefuse(@RequestParam Integer infoId, @RequestParam Integer raceId, HttpServletRequest request) {
        try {
            paticipantService.resultRefuse(infoId, raceId, request);
            return AjaxResultHandler.Success("成绩驳回");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * Add participant page param map.
     *
     * @param participantPageParamDto the participant page param dto
     * @return the map
     */
    @PostMapping("participant/page/param/add")
    public Map<String, Object> addParticipantPageParam(@RequestBody ParticipantPageParamDto participantPageParamDto) {
        try {
            paticipantService.addParticipantPageParam(participantPageParamDto);
            return AjaxResultHandler.Success("报名页面参数设定成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Gets participant page param by event id.
     *
     * @param eventId the event id
     * @return the participant page param by event id
     */
    @PostMapping("/participant/page/param/get/by/eventId")
    public Map<String, Object> getParticipantPageParamByEventId(@RequestParam("eventId") Integer eventId) {
        try {
            ParticipantPageParam participantPageParamByEventInfoId = paticipantService.getParticipantPageParamByEventId(eventId);
            return AjaxResultHandler.Success("获取报名页面参数成功", participantPageParamByEventInfoId);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Gets page 2 info.
     *
     * @param raceId the race id
     * @return the page 2 info
     */
    @GetMapping("/part2/getPage2Info")
    public Map<String, Object> getPage2Info(@RequestParam("raceId") Integer raceId) {
        try {
            return AjaxResultHandler.Success("获取数据成功", paticipantService.getPage2Info(raceId));
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Gets all participant data by event id and status.
     *
     * @param allParticipantDataByEventIdDto the all participant data by event id dto
     * @return the all participant data by event id and status
     */
    @PostMapping("/participant/getAllParticipantData/by/eventId/status/page")
    public Map<String, Object> getAllParticipantDataByEventIdAndStatus(@Validated @RequestBody AllParticipantDataByEventIdDto allParticipantDataByEventIdDto) {
        try {
            if (ObjectUtil.isNullObj(allParticipantDataByEventIdDto.getStatus()) || CollectionUtils.isEmpty(allParticipantDataByEventIdDto.getStatus())) {
                return AjaxResultHandler.Failed("状态必须传，不能为空");
            }
            return AjaxResultHandler.Success("获取数据成功", paticipantService.getAllParticipantDataByEventIdAndStatus(allParticipantDataByEventIdDto));
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * Gets participant code.
     *
     * @param raceId the race id
     * @return the participant code
     */
    @GetMapping("/event/getParticipantCode/{raceId}")
    public String getParticipantCode(@PathVariable("raceId") Integer raceId) {
        return paticipantService.getParticipantCode(raceId);
    }


    @PostMapping("/participant/update/race/part/code")
    public Map<String, Object> updateRacePartCode(@RequestParam("raceId") Integer raceId,
                                                  @RequestParam("code") String code) {
        try {
            paticipantService.updateRacePartCode(raceId, code);
            return AjaxResultHandler.Success("成功");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @PostMapping("/get/race/group/part/code")
    public Map<String, Object> getRaceInfoByPartCode(@RequestParam("eventId") Integer eventId,
                                                     @RequestParam("code") String code) {
        try {
            return AjaxResultHandler.Success("成功", paticipantService.getRaceGroupByPartCode(eventId, code));
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    @PostMapping("/participant/all/data/by/openId/eventId")
    public Map<String, Object> getAllInfoByOpenIdAndEventId(@RequestParam("eventId") Integer eventId,
                                                            @RequestParam("openId") String openId) {
        try {
            AllParticipantDataDto allParticipantDataDto = new AllParticipantDataDto();
            allParticipantDataDto.setEventId(eventId);
            allParticipantDataDto.setOpenId(openId);
            Map allInfoByOpenIdAndEventId = paticipantService.getAllInfoByOpenIdAndEventId(allParticipantDataDto);
            return AjaxResultHandler.Success("成功", allInfoByOpenIdAndEventId);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @PostMapping("/participant/numberbook/create")
    public Map<String, Object> getNumberBookByOpenIdAndEventId(@RequestParam("eventId") Integer eventId,
                                                               @RequestParam("personalMobile") String personalMobile, HttpServletResponse response) {
        try {
            GetParticipantDataByPersonalMobileAndEventIdDto getParticipantDataByDocumentNumAndEventIdDto = new GetParticipantDataByPersonalMobileAndEventIdDto();
            getParticipantDataByDocumentNumAndEventIdDto.setEventId(eventId);
            getParticipantDataByDocumentNumAndEventIdDto.setPersonalMobile(personalMobile);
            String numberBookByOpenIdAndEventId = paticipantService.getNumberBookByPersonMobileAndEventId(getParticipantDataByDocumentNumAndEventIdDto, response);
            return AjaxResultHandler.Success("成功", numberBookByOpenIdAndEventId);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    @PostMapping("/participant/race/certificate")
    public Map<String, Object> getRaceCertificate(@RequestParam("eventId") Integer eventId,
                                                  @RequestParam("code") String code, HttpServletResponse response) {
        try {
            String getRaceCertificate = paticipantService.getRaceCertificate(eventId, code, response);
            return AjaxResultHandler.Success("成功", getRaceCertificate);
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    @PostMapping("/participant/update/best/score/pic")
    public Map<String, Object> updateBestScorePic(@RequestBody UpdateBestScorePicDto updateBestScorePicDto, HttpServletResponse response) {
        try {
            paticipantService.updateBestScorePic(updateBestScorePicDto, response);
            return AjaxResultHandler.Success("上传成功，待审核通过后有短信提示，可下载完赛证书");
        } catch (Exception e) {
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

}
